/**
 * @description 公司编辑弹窗
 * @author 李艳
 */

import React, {Component} from 'react'
import {Form, Input, InputNumber, Tabs,Modal, Checkbox} from 'antd';
import { getUserInfo} from "../../../commons/utils/CommonUtils";

const FormItem = Form.Item;
const TabPane = Tabs.TabPane;

class EditCorporationModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      modalVisible: false,
      confirmLoading: false,
    }
  }

  handleClose = () => {
    this.props.form.resetFields();
  };

  componentDidMount() {
    this.props.onRef(this);
  }

  render() {
    const formItemLayout = {
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 15
      },
    };

    const {confirmLoading, modalVisible, handleOk, handleCancel, defaultValue, isAdd} = this.props;
    const {getFieldDecorator} = this.props.form;
    let title = "编辑";
    let FormValue = defaultValue;
    if (isAdd) {
      title = "新增";
      FormValue = {}
    }
    const {tenantCode = ""} = getUserInfo();
    return (
      <div>
        <Modal title={title}
                       visible={modalVisible}
                       onOk={handleOk}
                       onCancel={handleCancel}
                       width={600}
                       afterClose={this.handleClose}
                       confirmLoading={confirmLoading}
                       maskClosable={false}
        >
          <Tabs defaultActiveKey="1" type="card">
            <TabPane tab="通用" key="1">
              <FormItem
                style={{display: "none"}}
                label="id">
                {getFieldDecorator('id', {
                  initialValue: FormValue.id ? FormValue.id : "",
                })(
                  <Input/>
                )}
              </FormItem>
              <FormItem
                label={"代码"}
                {...formItemLayout}
              >
                {getFieldDecorator('code', {
                  initialValue: FormValue.code ? FormValue.code.substring(FormValue.code.indexOf('-') + 1, FormValue.code.length) : "",
                  rules: [{required: true, pattern: '^[A-Za-z0-9]{1,20}$', message: '代码仅支持字母和数字!'}]
                })(
                  <Input addonBefore={`${tenantCode}-`}
                         maxLength={tenantCode ? 20 - tenantCode.split("").length - 1 : 20}/>
                )}
              </FormItem>
              <FormItem
                {...formItemLayout}
                label="名称">
                {getFieldDecorator('name', {
                  initialValue: FormValue.name ? FormValue.name : "",
                  rules: [{required: true,whitespace:true, max: 50, message: '请填写名称(50个字以内)!'}]
                })(
                  <Input/>
                )}
              </FormItem>
              <FormItem
                {...formItemLayout}
                label="ERP公司代码">
                {getFieldDecorator('erpCode', {
                  initialValue: FormValue.erpCode ? FormValue.erpCode : "",
                  rules: [{required: true,pattern: '^[A-Za-z0-9]{1,10}$', message: '仅允许输入字母和数字(不超过10个字符)!'}]
                })(
                  <Input/>
                )}
              </FormItem>
              <FormItem
                {...formItemLayout}
                label="本位币货币名称">
                {getFieldDecorator('baseCurrencyName', {
                  initialValue: FormValue.baseCurrencyName ? FormValue.baseCurrencyName : "",
                  rules: [{required: true,whitespace:true, max: 20, message: '请填写本位币货币名称（20个字以内）!'}]
                })(
                  <Input/>
                )}
              </FormItem>
              <FormItem
                {...formItemLayout}
                label="本位币货币代码">
                {getFieldDecorator('baseCurrencyCode', {
                  initialValue: FormValue.baseCurrencyCode ? FormValue.baseCurrencyCode : "",
                  rules: [{required: true, pattern: '^[A-Za-z0-9]{1,5}$', message: '本位币货币代码允许输入字母和数字（5位以内）!'}]
                })(
                  <Input/>
                )}
              </FormItem>
              <FormItem
                {...formItemLayout}
                label="默认贸易伙伴代码">
                {getFieldDecorator('defaultTradePartner', {
                  initialValue: FormValue.defaultTradePartner ? FormValue.defaultTradePartner : "",
                })(
                  <Input/>
                )}
              </FormItem>
              <FormItem
                {...formItemLayout}
                label="关联交易贸易伙伴">
                {getFieldDecorator('relatedTradePartner', {
                  initialValue: FormValue.relatedTradePartner ? FormValue.relatedTradePartner : "",
                })(
                  <Input/>
                )}
              </FormItem>
              <FormItem
                {...formItemLayout}
                label="排序">
                {getFieldDecorator('rank', {
                  initialValue: FormValue.rank || FormValue.rank === 0 ? FormValue.rank : "",
                })(
                  <InputNumber precision={0} min={0} max={999999999} style={{width: "100%"}}/>
                )}
              </FormItem>
              <FormItem
                label={"冻结"}
                {...formItemLayout}
              >
                {getFieldDecorator('frozen', {
                  initialValue: FormValue.frozen,
                  valuePropName: "checked"
                })(
                  <Checkbox/>
                )}
              </FormItem>
            </TabPane>
            <TabPane tab="微信" key="2">
              <FormItem
                {...formItemLayout}
                label="微信用户凭证">
                {getFieldDecorator('weixinAppid', {
                  initialValue: FormValue.weixinAppid ? FormValue.weixinAppid : "",
                })(
                  <Input/>
                )}
              </FormItem>
              <FormItem
                {...formItemLayout}
                label="微信用户凭证密钥">
                {getFieldDecorator('weixinSecret', {
                  initialValue: FormValue.weixinSecret ? FormValue.weixinSecret : "",
                })(
                  <Input/>
                )}
              </FormItem>
            </TabPane>
          </Tabs>
        </Modal>
      </div>
    );
  }
}

EditCorporationModal = Form.create()(EditCorporationModal);
export default EditCorporationModal;
